package com.hit.demo1.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hit.demo1.entity.E_Stu;
import com.hit.demo1.entity.Kcrentity;
import com.hit.demo1.mapper.StrMapper;
import com.hit.demo1.service.StuService;
import com.hit.demo1.param.StuParam;
import com.hit.demo1.mapper.KcEntityMapper;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    public StrMapper strMapper;
    @Autowired
    public KcEntityMapper kcEntityMapper;


//    @Override
//    public Page<E_Stu> findAll(StuParam stuParam, int page, int limit) {
//        Specification<E_Stu> spe = new Specification<E_Stu>() {
//            @Override
//            public jakarta.persistence.criteria.Predicate toPredicate(Root<E_Stu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//
//                List<jakarta.persistence.criteria.Predicate> list = new ArrayList<>();
//                if(ObjectUtil.isNotEmpty(stuParam.getS1())){
//                    list.add(cb.and(cb.equal(root.get("sname"),stuParam.getS1())));
//                }
//                if(ObjectUtil.isNotEmpty(stuParam.getS2())){
//                    if(ObjectUtil.isNotEmpty(stuParam.getIsLike())){
//                        list.add(cb.and(cb.like(root.get("saddress"),"%"+stuParam.getS2()+"%")));
//                    }else{
//                        list.add(cb.and(cb.equal(root.get("saddress"),stuParam.getS2())));
//                    }
//                }
//                if(ObjectUtil.isNotEmpty(stuParam.())){
//                    list.add(cb.and(cb.equal(root.get("sclass"),stuParam.getSclass())));
//                }
//                if(ObjectUtil.isNotEmpty(stuParam.getSid())){
//                    list.add(cb.and(cb.equal(root.get("sid"),stuParam.getSid())));
//                }
//                jakarta.persistence.criteria.Predicate[] p = new jakarta.persistence.criteria.Predicate[list.size()];
//                return cb.and(list.toArray(p));
//
//            }
//        };
//        return strMapper.findAll(spe, PageRequest.of(page,limit));
//    }

    @Override
    public List<Kcrentity> findAll(Kcrentity kcEntity) {
        Specification<Kcrentity> spe = new Specification<Kcrentity>() {
            @Override
            public jakarta.persistence.criteria.Predicate toPredicate(Root<Kcrentity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<jakarta.persistence.criteria.Predicate> cbList = new ArrayList<>();
                if(ObjectUtil.isNotEmpty(kcEntity.getSgrade())){
                    cbList.add(cb.and(cb.equal(root.get("fs"),kcEntity.getSgrade())));
                }
                jakarta.persistence.criteria.Predicate[] p = new Predicate[cbList.size()];
                return cb.and(cbList.toArray(p));
            }
        };
        return kcEntityMapper.findAll(spe);
    }

    @Override
    public List<E_Stu> findAllBySclass(String sclass) {
        return strMapper.findAllBySclass(sclass);
    }
    @Override
    public List<E_Stu> findAllBySclassLike(String sclass) {
        return strMapper.findAllBySclassLike("%" + sclass + "%");
    }

    @Override
    public List<E_Stu> findAllBySaddressAndSclassOrderBySid(String saddress, String sclass) {
        return strMapper.findAllBySaddressAndSclassOrderBySid(saddress, sclass);
    }


    @Override
    public E_Stu save(E_Stu stu) {
        return strMapper.save(stu);
    }

    @Override
    public E_Stu update(E_Stu stu) {
        E_Stu oldSut = strMapper.findById(stu.getSid()).orElse(null);
        if(null==oldSut){
            return null;
        }
        stu.setSbirth(oldSut.getSbirth());
        return strMapper.save(stu);
    }

    @Override
    public int delete(Integer id) {
        strMapper.deleteById(id);
        return 1;
    }

    @Override
    public String insertNum(int number){
        for (int i =0; i< number; i++){
            E_Stu stu = new E_Stu();
            stu.setSname("name"+i);
            stu.setSaddress("address"+i);
            stu.setSclass("class"+i);
            stu.setSbirth(DateUtil.date().toSqlDate());
            strMapper.save(stu);
        }
        return "success";
    }
    @Override
    public int delete(String ids) {
        for(String id:ids.split(",")){
            strMapper.deleteById(Integer.parseInt(id));
        }
        return 0;
    }



}
